import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
public class StudentManagementSystem {
    private static List<Classroom> classrooms = new ArrayList<>();
    private static Scanner scanner = new Scanner(System.in);
    private static Random random = new Random();

    public static void main(String[] args) {
        while (true) {
            System.out.println("1. 班级管理  2. 小组管理  3. 学生管理  4. 随机点名  5. 退出");
            int choice = scanner.nextInt();
            scanner.nextLine(); // Consume the newline character

            switch (choice) {
                case 1:
                    manageClassroom();
                    break;
                case 2:
                    manageGroup();
                    break;
                case 3:
                    manageStudent();
                    break;
                case 4:
                    randomCalling();
                    break;
                case 5:
                    System.exit(0);
                    break;
                default:
                    System.out.println("无效选择，请重试。");
            }
        }
    }

    private static void manageClassroom() {
        System.out.println("请输入班级名称：");
        String className = scanner.nextLine();
        classrooms.add(new Classroom(className));
        System.out.println("班级 " + className + " 添加成功！");
    }

    private static void manageGroup() {
        System.out.println("请输入班级名称以添加小组：");
        String className = scanner.nextLine();
        Classroom classroom = findClassroom(className);
        if (classroom != null) {
            System.out.println("请输入小组名称：");
            String groupName = scanner.nextLine();
            classroom.addGroup(new Group(groupName));
            System.out.println("小组 " + groupName + " 添加成功！");
        } else {
            System.out.println("未找到该班级！");
        }
    }

    private static void manageStudent() {
        System.out.println("请输入班级名称以添加学生：");
        String className = scanner.nextLine();
        Classroom classroom = findClassroom(className);
        if (classroom != null) {
            System.out.println("请输入小组名称以添加学生：");
            String groupName = scanner.nextLine();
            Group group = findGroup(classroom, groupName);
            if (group != null) {
                System.out.println("请输入学生的名字：");
                String name = scanner.nextLine();
                System.out.println("请输入学生的年龄：");
                String age = scanner.nextLine();
                System.out.println("请输入学生的学号：");
                String sno = scanner.nextLine();
                group.addStudent(new Student(name, age, sno));
                System.out.println("学生 " + name + " 添加成功！");
            } else {
                System.out.println("未找到该小组！");
            }
        } else {
            System.out.println("未找到该班级！");
        }
    }

    private static void randomCalling() {
        System.out.println("请输入班级名称以进行随机点名：");
        String className = scanner.nextLine();
        Classroom classroom = findClassroom(className);
        if (classroom != null) {
            Group group = randomChoice(classroom.getGroups());
            if (group != null) {
                Student student = randomChoice(group.getStudents());
                if (student != null) {
                    System.out.println("随机点名结果：小组 " + group.getGroupName() + " 的学生 " + student.getName());
                } else {
                    System.out.println("该小组没有学生。");
                }
            } else {
                System.out.println("该班级没有小组。");
            }
        } else {
            System.out.println("未找到该班级！");
        }
    }

    private static Classroom findClassroom(String name) {
        for (Classroom classroom : classrooms) {
            if (classroom.getClassName().equals(name)) {
                return classroom;
            }
        }
        return null;
    }

    private static Group findGroup(Classroom classroom, String groupName) {
        for (Group group : classroom.getGroups()) {
            if (group.getGroupName().equals(groupName)) {
                return group;
            }
        }
        return null;
    }

    private static <T> T randomChoice(List<T> list) {
        if (list.isEmpty()) {
            return null;
        }
        return list.get(random.nextInt(list.size()));
    }
}
